import router from '@/router/index'
import {
	recursionRouter
} from '@/utils/recursion-router'
import dynamicRouter from '@/router/dynamicRouter'
import API from '@/api/login.js'

const Layout = () => import('@/components/layout/index');

const Home = {
	path: '/home',
	name: 'home',
	component: () => import('@/views/home/home'),
	// component: resolve => require(['@/views/police/index'], resolve),
	meta: {
		title: '主页HOME',
		icon: 'el-icon-s-home'
	}
}

export default {
	namespaced: true,
	state: {
		permissionList: null /** 所有路由 */ ,
		sidebarMenu: [] /** 导航菜单 */ ,
		currentMenu: '' /** 当前active导航菜单 */ ,
		control_list: [] /** 完整的权限列表 */ ,
	},
	mutations: {
		SET_PERMISSION(state, routes) {
			state.permissionList = routes
		},
		CLEAR_PERMISSION(state) {
			state.permissionList = null
		},
		SET_MENU(state, menu) {
			state.sidebarMenu = menu
		},
		CLEAR_MENU(state) {
			state.sidebarMenu = []
		},
		SET_CURRENT_MENU(state, currentMenu) {
			state.currentMenu = currentMenu
		},
		SET_CONTROL_LIST(state, list) {
			state.control_list = list
		}
	},

	actions: {
		async FETCH_PERMISSION({
			commit
		}) {
			try {
				let res = await API.permissions();
				console.log(res.data)
				let arr = res.data.data;
				// let arr = ["baiduaip","baiduaipAdd","baiduaipGet","baiduaipDel","project","projectAdd","projectGet","projectDel","address","addressAdd","addressGet","addressDel","face","faceAdd","faceGet","faceDel","device","deviceAdd","deviceGet","deviceDel","package","apkAdd","apkDel","apkGet","apkVersionAdd","apkVersionDel","apkVersionSend","danger","dangerAdd","dangerDel","dangerGet","dangerLogGet","dangerLogDel","permission","roleGet","roleAdd","roleEdit","roleDel","manageGet","manageAdd","manageEdit","manageDel","manageResetPwd","doubtable","dangerlogs","overview","areas","addressList","deviceList","command","outInLogs","roles","user","pushLogs","faceList","manage","area","areaList","userList","renter","renterList","school","schoolList","information","infoList","switch","faceSwitch","policeList","department","police","policeListAdd","policeListGet","policeListEdit","policeListDel","departmentAdd","departmentGet","departmentEdit","departmentDel","security","upload","search","account","alert","statistical","data","generate","personVerifies","dangerLogGetStation","screenSaver","alertGet"]
				let routes = recursionRouter(arr, dynamicRouter)
				let DynamicRoutes = {
					path: '',
					name: 'layout',
					component: Layout,
					redirect: '/home',
					children: [Home]
				};
				
				DynamicRoutes.children.push(...routes);
				router.addRoute('', DynamicRoutes)
				commit('SET_CONTROL_LIST', [...DynamicRoutes.children, ...dynamicRouter])
				commit('SET_MENU', DynamicRoutes.children)
				let initialRoutes = router.options.routes + router.options.routes.DynamicRoutes;
				commit('SET_PERMISSION', [...initialRoutes])
				
				// DynamicRoutes.children = routes;
				// router.addRoute('', DynamicRoutes)
				// commit('SET_CONTROL_LIST', [...DynamicRoutes.children, ...dynamicRouter])
				// commit('SET_MENU', DynamicRoutes.children)
				// let initialRoutes = router.options.routes + router.options.routes.DynamicRoutes;
				// commit('SET_PERMISSION', [...initialRoutes])
			} catch {
				// window.location.reload()
				localStorage.removeItem('token')
				this.$router.replace('/login')
			}

		}
	}
}
